<?php

/**
 * This is the model class for table "tabuleiro".
 *
 * The followings are the available columns in table 'tabuleiro':
 * @property integer $id
 * @property string $descricao
 * @property integer $num_alveolos
 * @property string $cubicagem
 * @property string $comprimento
 * @property string $largura
 * @property integer $active
 * @property string $created_at
 * @property string $updated_at
 *
 * The followings are the available model relations:
 * @property ProdutoTabuleiro[] $produtoTabuleiros
 */
class Tabuleiro extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Tabuleiro the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'tabuleiro';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('descricao, num_alveolos, cubicagem, comprimento, largura', 'required'),
            array('num_alveolos, active', 'numerical', 'integerOnly' => true),
            array('descricao', 'length', 'max' => 255),
            array('cubicagem', 'length', 'max' => 20),
            array('comprimento, largura', 'length', 'max' => 10),
            array('created_at, updated_at', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, descricao, num_alveolos, cubicagem, comprimento, largura, active, created_at, updated_at', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'produto' => array(self::MANY_MANY, 'Produto', 'tabuleiro_produto(id_tabuleiro,id_produto)'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'descricao' => 'Descricao',
            'num_alveolos' => 'Num Alveolos',
            'cubicagem' => 'Cubicagem',
            'comprimento' => 'Comprimento',
            'largura' => 'Largura',
            'active' => 'Active',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('descricao', $this->descricao, true);
        $criteria->compare('num_alveolos', $this->num_alveolos);
        $criteria->compare('cubicagem', $this->cubicagem, true);
        $criteria->compare('comprimento', $this->comprimento, true);
        $criteria->compare('largura', $this->largura, true);
        $criteria->compare('active', $this->active);
        $criteria->compare('t.created_at', $this->created_at, true);
        $criteria->compare('t.updated_at', $this->updated_at, true);
        $criteria->with = array("produto");

        //$criteria->compare('produto.id_produto', $id, true);
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

}
